@extends('shop::layouts.master')
@section('title')
    安全设置
@endsection
@section('tab')
    <div class="layui-fluid flex-direction-column">
        <div>
            <div  class="flex-direction-row" style="padding: 20px;">
                <div style="margin-right: 20px;" class="layui-inline">
                    @if(!empty($arr_data['avatar']['full_src']))
                        <img class="layui-circle" src="{{$arr_data['avatar']['full_src']}}" alt="" height="100" width="100">
                    @else
                        <img class="layui-circle" src="/admin/images/shop_user_default.png" alt="" height="100" width="100">
                    @endif
                </div>

                <div class="flex-direction-column">
                    <div style="margin-bottom: 10px;">{{$arr_data['username']}}</div>
                    <div style="margin-bottom: 10px;" class="上次登录时间">上次登录时间：{{$arr_data['login_data'][0]['login_at']}}</div>
                    <div style="margin-bottom: 10px;" class="上次登录ip">上次登录ip：{{$arr_data['login_data'][0]['ip']}}</div>
                    <div class="flex-direction-row">
                        <div style="margin:0 10px 5px 0;">安全级别 ( {{$arr_data['safety_data']['safety']}} )</div>
                        @if($arr_data['safety_data']['safety'] === '弱')
                            <div class="color-red">{{$arr_data['safety_data']['notice']}}</div>
                        @elseif($arr_data['safety_data']['safety'] === '中等')
                            <div class="color-orange">{{$arr_data['safety_data']['notice']}}</div>
                        @elseif($arr_data['safety_data']['safety'] === '强')
                            <div class="color-green">{{$arr_data['safety_data']['notice']}}</div>
                        @elseif($arr_data['safety_data']['safety'] === '极好')
                            <div class="color-green">{{$arr_data['safety_data']['notice']}}</div>
                        @else
                            <div>{{$arr_data['safety_data']['notice']}}</div>
                        @endif
                    </div>
                    <div class="layui-progress layui-progress-big">
                        @if($arr_data['safety_data']['safety'] === '弱')
                            <div class="layui-progress-bar backgroundcolor-red" lay-percent="{{$arr_data['safety_data']['percent']}}"></div>
                        @elseif($arr_data['safety_data']['safety'] === '中等')
                            <div class="layui-progress-bar backgroundcolor-orange" lay-percent="{{$arr_data['safety_data']['percent']}}"></div>
                        @elseif($arr_data['safety_data']['safety'] === '强')
                            <div class="layui-progress-bar backgroundcolor-green" lay-percent="{{$arr_data['safety_data']['percent']}}"></div>
                        @elseif($arr_data['safety_data']['safety'] === '极好')
                            <div class="layui-progress-bar backgroundcolor-green" lay-percent="{{$arr_data['safety_data']['percent']}}"></div>
                        @else
                            <div></div>
                        @endif
                    </div>
                </div>
            </div>
        </div>

        <hr>

        <div >
            <div style="padding: 20px 20px 0 20px;">
                <div class="layui-fluid flex-direction-row justify-content-space-between">
                    <div class=" flex-direction-row" style="line-height: 30px">
                        <div class="color-green" style="padding-right: 10px;">已设置</div>
                        <div style="padding-right: 10px;">登录密码</div>
                        <div style="color: grey;">登录密码用于会员的登录以及进行登录的一系列操作，建议您定期更改密码</div>
                    </div>

                    <div class="操作">
                        @if($arr_data['safety_data']['safety'] === '弱' || $arr_data['safety_data']['safety'] === '中等')
                            <button class="layui-btn layui-btn-sm backgroundcolor-red" onclick="edit_password()" ><i class="layui-icon">&#xe642;</i>立即修改</button>


                        @else
                            <button class="layui-btn layui-btn-sm " onclick="edit_password()"><i class="layui-icon">&#xe642;</i>立即修改</button>

                        @endif

                    </div>

                </div>
            </div>


            <div style="padding: 20px 20px 0 20px;">
                <div class="layui-fluid flex-direction-row justify-content-space-between">
                    <div class=" flex-direction-row" style="line-height: 30px">
                        <div class="color-green" style="padding-right: 10px;">已设置</div>
                        <div style="padding-right: 10px;">手机验证</div>
                        <div style="color: grey;">手机验证方便登录以及一些安全性操作。</div>
                    </div>

                    <div class="操作">
                        <button class="layui-btn layui-btn-sm" onclick="edit_phone()"><i class="layui-icon">&#xe642;</i>立即修改</button>

                    </div>

                </div>
            </div>

            @if($arr_data['is_master'] === 1)
                <div style="padding: 20px 20px 0 20px;">
                    <div class="layui-fluid flex-direction-row justify-content-space-between">
                        <div class=" flex-direction-row" style="line-height: 30px">
                            @if(!empty($arr_data['shop_base']['pay_password']))
                                <div class="color-green" style="padding-right: 10px;">已设置</div>
                            @else
                                <div class="color-red" style="padding-right: 10px;">未设置</div>
                            @endif
                            <div style="padding-right: 10px;">支付密码</div>
                            <div style="color: grey;" >
                                @if(!empty($arr_data['shop_base']['pay_password']))
                                    支付密码用于会员的提现账号设置及提现申请操作。
                                @else
                                    支付密码用于会员的提现账号设置及提现申请操作，您还没有设置支付密码
                                @endif
                            </div>
                        </div>

                        <div class="操作">
                            @if(!empty($arr_data['shop_base']['pay_password']))
                                <button class="layui-btn layui-btn-sm" onclick="edit_pay_password()"><i class="layui-icon">&#xe642;</i>立即修改</button>

                            @else
                                <button class="layui-btn layui-btn-sm backgroundcolor-red" onclick="set_pay_password()" ><i class="layui-icon">&#xe642;</i>立即设置</button>
                            @endif
                        </div>
                    </div>
                </div>
            @endif
        </div>
    </div>

@endsection

{{--修改登录密码--}}
<div id="edit_password_html" style="display:none;">
    <form class="layui-form" lay-filter="edit_password_form">
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>原始密码</label>
            <div class="layui-input-inline">
                <input type="text" name="password" required="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="new_password" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>确认密码</label>
            <div class="layui-input-inline">
                <input type="password" name="re_new_password" required="" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label"></label>
            <input type="hidden" id="L_id" name="id">
            <button class="layui-btn"   lay-filter="edit_password_submit" lay-submit="">确定</button>
        </div>
    </form>
</div>

{{--修改登录手机--}}
<style>
    .send-code{
        width: 90px;
        height: 30px;
        padding: 0 5px;
        margin: 0;
        font-size: 14px;
        text-align: center;
        background: transparent;
        border-radius: 30px;
        color: #a07941;
        border-color: #a07941;

    }
    .layui-send-code {
        float: left;
        display: block;
        line-height: 20px;
        margin-right: 10px;
    }
    .button-disable{
        cursor:not-allowed;
        -webkit-box-shadow:none;
        box-shadow:none;
        opacity:.65;
    }
</style>
<div id="edit_phone_html" style="display:none;">
    <form class="layui-form" lay-filter="edit_phone_form">
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>原手机号</label>
            <div class="layui-input-inline">
                <input type="text" id="phone" name="phone" required="" disabled autocomplete="off" class="layui-input">
            </div>
            <div class="layui-send-code">
                <input type="button" class="btn btn-default send-code" value="获取验证码" onClick="send_code($(this),'phone')" />
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>验证码</label>
            <div class="layui-input-inline">
                <input type="text" name="code" required="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>新手机号</label>
            <div class="layui-input-inline">
                <input type="text" id="new_phone"  name="new_phone" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-send-code">
                <input type="button" class="btn btn-default send-code" value="获取验证码" onClick="send_code($(this),'new_phone')" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>新手机验证码</label>
            <div class="layui-input-inline">
                <input type="text" name="new_code" required="" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label"></label>
            <input type="hidden" id="L_id" name="id">
            <button class="layui-btn"   lay-filter="edit_phone_submit" lay-submit="">确定</button>
        </div>
    </form>
</div>

{{--设置支付密码--}}
<div id="set_pay_password_html" style="display:none;">
    <form class="layui-form" lay-filter="set_pay_password_form">
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>支付密码</label>
            <div class="layui-input-inline">
                <input type="password" name="new_password" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>确认密码</label>
            <div class="layui-input-inline">
                <input type="password" name="re_new_password" required="" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label"></label>
            <input type="hidden" id="L_id" name="id">
            <button class="layui-btn"   lay-filter="set_pay_password_submit" lay-submit="">确定</button>
        </div>
    </form>
</div>

@section('tab_js')
    <script>
        /*检测修改登录密码前数据*/
        function check_form_data_for_edit_password($_data,real_password) {
            var s_msg = '';
            if($_data.password.length <= 0){
                s_msg = '请输入原始密码';
                return s_msg;
            }
            if(real_password !== $_data.password){
                s_msg = '原始密码不正确';
                return s_msg;
            }
            if($_data.new_password.length <= 0){
                s_msg = '请输入新密码';
                return s_msg;
            }
            if(!($_data.new_password.length <= 18 && $_data.new_password.length >= 6)){
                s_msg = '请输入6-18位的密码';
                return s_msg;
            }
            if(real_password === $_data.new_password){
                s_msg = '请不要设置和原始密码一样的密码';
                return s_msg;
            }
            if($_data.re_new_password !== $_data.new_password){
                s_msg = '2次输入密码不一致';
                return s_msg;
            }
            return s_msg;
        }

        /*修改登录密码*/
        function edit_password() {
            var ss = 'edit_password';
            var s_id = ss+'_html';
            var s_form = ss+'_form';
            var s_submit = ss+'_submit';
            var html_index = layer.open({
                title: '修改登录密码',
                area: ['400px', '300px'],
                type: 1,
                content:$("#"+s_id) //这里content是一个普通的String
            });


            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;

                    //清空表单数据值
                    form.val(s_form, {
                        id: "",
                        password: "",
                        new_password: "",
                        re_new_password: "",

                    });

                    //监听提交
                    form.on('submit('+s_submit+')',
                        function(data) {
                            var form_data = data.field;
                            var real_password = "{{$arr_data['pass']}}";
                            var msg = check_form_data_for_edit_password(form_data,real_password);
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('shop_safety_update_password')}}';
                            var $_data = {
                                id:form_data.id,
                                pass:form_data.new_password,
                            };
                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        layer.msg(
                                            "修改成功",
                                            {time:500,icon: 6},
                                            function() {
                                                //关闭当前frame
                                                layer.close(html_index);
                                                location.reload()

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //添加同级权限表单初始赋值
                    form.val(s_form, {
                        "id": "{{$arr_data['id']}}",

                    })

                });
        }

        /*发送验证码*/
        function send_code($_this,$_id) {
            var send_code_seconds = 60;//60秒才能发送一次验证码
            var phone = $.trim($('#'+$_id).val());
            var phoneReg = /^1[3-9]\d{9}$/;//手机号正则

            if (!phoneReg.test(phone)) {
                layer.msg(" 请输入有效的手机号码");
                return false;
            }
            //设置button效果，开始计时
            $_this.attr("disabled", "true");
            $_this.addClass("button-disable");
            $_this.val( + send_code_seconds + "秒再获取");

            var $_url = '{{route('shop_send_phone_code')}}';
            var $_data = {phone:phone};
            $.ajax({
                url:$_url,
                type:'post',
                data:$_data,
                dataType : "json",
                success:function(result){
                    if(result.state === 0){
                        console.log(result,'result');
                        layer.msg("发送成功");
                    }else{
                        layer.msg(result.msg);
                    }
                }});
            var InterValObj1 = window.setInterval( function () {
                if (send_code_seconds <= 0) {
                    window.clearInterval(InterValObj1);//停止计时器
                    $_this.removeAttr("disabled");//启用按钮
                    $_this.removeClass("button-disable");
                    $_this.val("重新发送");
                }
                else {
                    send_code_seconds--;
                    $_this.val( + send_code_seconds + "秒再获取");
                }
            }, 1000); //启动计时器，1秒执行一次
        }

        /*检测修改登录手机前数据*/
        function check_form_data_for_edit_phone($_data) {
            var s_msg = '';
            if($_data.phone.length <= 0){
                s_msg = '请输入手机号';
                return s_msg;
            }
            if($_data.code.length <= 0){
                s_msg = '请输入原手机验证码';
                return s_msg;
            }
            if($_data.new_phone.length <= 0){
                s_msg = '请输入新手机号';
                return s_msg;
            }
            if($_data.new_code.length <= 0){
                s_msg = '请输入新手机验证码';
                return s_msg;
            }
            if($_data.phone === $_data.new_phone){
                s_msg = '请重新修改新手机号，不要和原手机号相同';
                return s_msg;
            }
            return s_msg;
        }

        /*修改登录手机*/
        function edit_phone() {
            var ss = 'edit_phone';
            var s_id = ss+'_html';
            var s_form = ss+'_form';
            var s_submit = ss+'_submit';
            var html_index = layer.open({
                title: '换绑手机',
                area: ['500px', '300px'],
                type: 1,
                content:$("#"+s_id) //这里content是一个普通的String
            });


            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;

                    //清空表单数据值
                    form.val(s_form, {
                        id: "",
                        phone: "",
                        code: "",
                        new_phone: "",
                        new_code: "",
                    });

                    //监听提交
                    form.on('submit('+s_submit+')',
                        function(data) {
                            var $_data = data.field;
                            var $_url = '{{route('shop_safety_update_phone')}}';
                            var msg = check_form_data_for_edit_phone($_data);
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        layer.msg(
                                            "修改成功",
                                            {time:500,icon: 6},
                                            function() {
                                                //关闭当前frame
                                                layer.close(html_index);
                                                location.reload()

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //表单初始赋值
                    form.val(s_form, {
                        "id": "{{$arr_data['id']}}",
                        "phone": "{{$arr_data['phone']}}",
                    })

                });
        }

        /*检测设置支付密码前数据*/
        function check_form_data_for_set_pay_password($_data) {
            var s_msg = '';
            if($_data.new_password.length <= 0){
                s_msg = '请输入支付密码';
                return s_msg;
            }
            if($_data.new_password !== $_data.re_new_password){
                s_msg = '2次输入密码不一致';
                return s_msg;
            }
            if(!($_data.new_password.length <= 18 && $_data.new_password.length >= 6)){
                s_msg = '请输入6-18位的支付密码';
                return s_msg;
            }
            return s_msg;
        }

        /*设置支付密码*/
        function set_pay_password() {
            var ss = 'set_pay_password';
            var s_id = ss+'_html';
            var s_form = ss+'_form';
            var s_submit = ss+'_submit';
            var html_index = layer.open({
                title: '设置支付密码',
                area: ['400px'],
                type: 1,
                content:$("#"+s_id) //这里content是一个普通的String
            });


            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;

                    //清空表单数据值
                    form.val(s_form, {
                        id: "",
                        new_password:"",
                        re_new_password:"",


                    });

                    //监听提交
                    form.on('submit('+s_submit+')',
                        function(data) {
                            var form_data = data.field;
                            var msg = check_form_data_for_set_pay_password(form_data);
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('shop_safety_pay_password_set')}}';
                            var $_data = {
                                id:form_data.id,
                                pay_password:form_data.new_password,
                            };
                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        layer.msg(
                                            "修改成功",
                                            {time:500,icon: 6},
                                            function() {
                                                //关闭当前frame
                                                layer.close(html_index);
                                                location.reload()

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //表单初始赋值
                    form.val(s_form, {
                        "id": "{{$arr_data['shop_base']['id']}}",

                    })

                });
        }

        /*修改支付密码*/
        function edit_pay_password() {
            var ss = 'edit_password';
            var s_id = ss+'_html';
            var s_form = ss+'_form';
            var s_submit = ss+'_submit';
            var html_index = layer.open({
                title: '修改支付密码',
                area: ['400px'],
                type: 1,
                content:$("#"+s_id) //这里content是一个普通的String
            });


            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;

                    //清空表单数据值
                    form.val(s_form, {
                        id: "",
                        password: "",
                        new_password: "",
                        re_new_password: "",
                    });

                    //监听提交
                    form.on('submit('+s_submit+')',
                        function(data) {
                            var form_data = data.field;
                            var real_password = "{{$arr_data['shop_base']['pay_password']}}";
                            var msg = check_form_data_for_edit_password(form_data,real_password);
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('shop_safety_pay_password_set')}}';
                            var $_data = {
                                id:form_data.id,
                                pay_password:form_data.new_password,
                            };
                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        layer.msg(
                                            "修改成功",
                                            {time:500,icon: 6},
                                            function() {
                                                //关闭当前frame
                                                layer.close(html_index);
                                                location.reload()

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //表单初始赋值
                    form.val(s_form, {
                        "id": "{{$arr_data['shop_base']['id']}}",

                    })

                });
        }
    </script>
@endsection




